<?php
class Controller_Dealing extends FLEA_Controller_Action
{

	function Controller_Dealing() {
		
	}
	function actionPosGetMemberMsg() {
		$onemember = & FLEA::getSingleton('Table_Members');
		$out = '{"OneMember":"failure","message":"会员id不正确!"}';
		//下面的参数是提交需要用的参数，可以在此进行变换、处理和安全检查
		$_consumer_id = isset($_POST['consumer_id'])?$_POST['consumer_id']:false;//提交的正在消费的客户ID
		$_terminal_id = isset($_POST['terminal_id'])?$_POST['terminal_id']:false;//此次提交的终端号
		//$_consumer_id = isset($_GET['consumer_id'])?$_GET['consumer_id']:false;
		//$_terminal_id = isset($_GET['terminal_id'])?$_GET['terminal_id']:false;

		if(false!==$_consumer_id and false!==$_terminal_id) {

			//通过上传的会员客户号获取会员记录
			__TRY();        
				$result = $onemember->find("consumer_id =".$_consumer_id ,null,"*",false);
			$ex = __CATCH();
			if (__IS_EXCEPTION($ex)) {
				$result = "获取会员信息失败!";
				$out = '{"OneMember":"failure","message":"'.$result.'"}';
			}
			if($result !== false) {
				//如果会员记录存在，进行优惠查询
				$hisPreferential = & $this->_getPreferential($_terminal_id,$_consumer_id,$result['level_id']);
				$out = '{OneMember:['.json_encode($result).'],Preferential:'.($hisPreferential==0?'null':json_encode($hisPreferential)).'}'; 
			}
		}//end 判断客户号数据上传正常
		echo $out;
	}
	function actionPosGetGoodsMsg() {
		$onegoods = & FLEA::getSingleton('Table_Goods');
		$out = '{"OneGoods":"failure","message":"商品id不正确!"}';
		//下面的参数是提交需要用的参数，可以在此进行变换、处理和安全检查
		$_consumer_id = isset($_POST['consumer_id'])?$_POST['consumer_id']:false;//此次提交的正在消费的客户号,可以为空（非会员）
		$_member_level= isset($_POST['member_level'])?$_POST['member_level']:false;//此次提交的正在消费的会员级别,可以为空（非会员）
		$_terminal_id = isset($_POST['terminal_id'])?$_POST['terminal_id']:false;//此次提交的终端号
		$_goods_barcode=isset($_POST['goods_barcode'])?$_POST['goods_barcode']:false;//此次提交的商品ID
		//$_consumer_id = isset($_GET['consumer_id'])?$_GET['consumer_id']:false;
		//$_member_level= isset($_GET['member_level'])?$_GET['member_level']:false;
		//$_terminal_id = isset($_GET['terminal_id'])?$_GET['terminal_id']:false;
		//$_goods_barcode=isset($_GET['goods_barcode'])?$_GET['goods_barcode']:false;

		if(false !== $_consumer_id or '' ===$_consumer_id) { 
			$_member_level = 'null';
			$_consumer_id = 'null';
		}; 

		if(false!==$_goods_barcode and false!==$_terminal_id) {
			__TRY();        
				$result = $onegoods->find("barcode =".$_goods_barcode,null,"*",false);
			$ex = __CATCH();
			if (__IS_EXCEPTION($ex)) {
				$result = "获取商品失败!";
				$out = '{"OneGoods":"failure","message":"'.$result.'"}';
			}
			if($result !== false) {
				//如果商品记录存在，进行优惠查询
				$itsPreferential = & $this->_getPreferential($_terminal_id,$_consumer_id,$_member_level,$result['goods_id']);
				$out = '{OneGoods:['.json_encode($result).'],Preferential:'.($itsPreferential==0?'null':json_encode($itsPreferential)).'}';
			}
		}
		echo $out;
	}

	//function actionPosDealing() {
	//}

	//private function
	//function:		获得优惠信息
	//parameter:	
	//				$t_id		//1. 终端id; 
	//				$c_id		//2. 会员客户号; 
	//				$m_level	//3. 会员级别;
	//				$g_id		//4. 商品id
	//return:		优惠数据集 ,包含排斥数组
	function _getPreferential($t_id,$c_id,$m_level,$g_id=null) {
		$pr = & FLEA::getSingleton('Table_Preferentials');
		$where = '
					`enabled` = true 
					and `start` <= now() 
					and `end` >= now() 
					and `goods_id` '.($g_id ===null?'is null':('='.$g_id)).'
					and	( `terminal_id` is null or `terminal_id` =  '.$t_id		.') '.'
					and ( `consumer_id` is null or `consumer_id` =  '.$c_id		.') '.'
					and	( `level`       is null or `level` 	     =  '.$m_level	.') '.'
					and	( `owner_level` is null or `owner_level` >= '.$m_level	.') '.'
				';
		$fields = '*';
		//echo $where;
		__TRY();
			$preferential = $pr->findAll($where,null,null,$fields,true);
		$ex = __CATCH();
		if(__IS_EXCEPTION($ex)) {
			$preferential = 0;
		}
		//如果优惠列表不为空
		if($preferential!=0) {
			foreach ($preferential as &$thisPre){
				//将互斥记录改写成数字索引的数组，便于js直接使用
				if($thisPre['excludes'] !=null) {
					$_s = array();
					foreach($thisPre['excludes'] as $thisEx){
						$_s[]=$thisEx['slave_preferential_id'];
					}
					$thisPre['excludes'] = $_s;
				}
				//如果礼品ID不为空，查询id的名字、库存量
				if(null !== $thisPre['gift_id']) {
					 $g = & FLEA::getSingleton('Table_Goods');
					__TRY();        
						 $r = $g->find("`goods_id` =".$thisPre['gift_id'],null,"totalnum ,name",false);
					$ex = __CATCH();
					if (__IS_EXCEPTION($ex)) {
						$r = false;
					}
					if($r) {
						$thisPre['gift_name'] = $r['name'];
						$thisPre['gift_totalnum'] = $r['totalnum'];
					}
				}
			}
			
		}

		return $preferential;
	}//end function _getPreferential()
}
?>